const express = require('express')
const app = express()
const cors = require('cors')
app.use(cors())

app.use(express.urlencoded({ extended: false }))
app.use(express.json())

// 路由之前封装中间件
app.use((req, res, next) => {
    res.cc = function (err, code = -1) {
        res.send({
            code,
            msg: err instanceof Error ? err.msg : err
        })
    }
    next()
})


const userRouter = require('./router/user.js')
app.use('/admin', userRouter)

const joi = require('joi')
app.use(function (err, req, res, next) {
    // 数据验证失败
    if (err instanceof joi.ValidationError) return res.cc(err)
    // 未知错误
    res.cc(err)
  })
  
app.listen(3007, function () {
    console.log('服务器启动');
})
